package com.xiaozhengkeji.colastartboot.services;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 微信小程序的工具集
 */
@Service("WXUtilsService")
@Slf4j
public class WXUtilsService {
    private static final String APP_ID = "";
    private static final String APP_SECRET = "";

    /**
     * 使用微信返回的LoginCode获取微信用户的唯一标识
     */
    public String getWXMarkByLoginCode(String loginCode) {
        HashMap<String, Object> paramMap = new LinkedHashMap<>();
        paramMap.put("appid", APP_ID);
        paramMap.put("secret", APP_SECRET);
        paramMap.put("js_code", loginCode);
        paramMap.put("grant_type", "authorization_code");
        String res = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramMap);
        Map<String, String> resMap = JSONObject.parseObject(res, Map.class);
        return resMap.get("openid");
    }
}
